package first.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file_sum {
        public static void main(String[] args) throws FileNotFoundException {
            String name = "E:/file.txt"; //文件名
            int chars=0; //字符数
            int line = 0;    //行数
            int words = 0;    //字母数
            try{
                File file=new File(name);
                BufferedReader br= new BufferedReader(new FileReader(file));
                String str = null;
                while((str=br.readLine())!=null){
                    System.out.println(str);
                    line++;
                    words += countWords(str);
                    chars += str.length();
                }

            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.println("字母数"+words);
            System.out.println("行数"+line);
            System.out.println("字符数"+chars);
        }
        public static int countWords(String str) {
            int count = 0;
            Pattern p = Pattern.compile("[a-zA-Z]");
            Matcher m = p.matcher(str);
            while(m.find())
                count++;
            return count;
        }

}

